using System;
using System.Collections.Generic;
using System.Text;
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace affectSQLObject
{
    class affectedObject
    {
        static void Main(string[] args)
        {
            int c = Environment.TickCount;

            if (args.Length == 0)
            {
                Console.WriteLine("{0} scriptfile", "syntaxcheck");
                return;
            }


            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
            sqlparser.Sqlfilename = args[0];
            int iRet = sqlparser.Parse();


            if (iRet == 0)
            {
                foreach (TCustomSqlStatement stmt in sqlparser.SqlStatements)
                {
                    checkStmt(stmt);
                }

            }
            else
            {
                Console.WriteLine("Syntax error found in input sql:");
                Console.WriteLine(sqlparser.ErrorMessages);

            }


        }

        static void checkStmt(TCustomSqlStatement pstmt)
        {
            switch (pstmt.SqlStatementType)
            {
                case TSqlStatementType.sstSelect:
                    TSelectSqlStatement selectstmt = (TSelectSqlStatement)pstmt;
                    String p = "";

                    for (int i = 0; i < selectstmt.Tables.Count(); i++)
                    {
                        p = p + selectstmt.Tables[i].AsText;
                        if (i != selectstmt.Tables.Count() - 1)
                        {
                            p = p + ",";
                        }
                    }

                    Console.WriteLine("select statement: {0}", p);

                    break;
                case TSqlStatementType.sstDelete:
                    TDeleteSqlStatement deletestmt = (TDeleteSqlStatement)pstmt;
                     p = "";

                    for (int i = 0; i < deletestmt.Tables.Count(); i++)
                    {
                        p = p +  deletestmt.Tables[i].AsText;
                        if (i != deletestmt.Tables.Count() - 1)
                        {
                            p = p + ",";
                        }
                    }

                    Console.WriteLine("delete statement: {0}",p);
                    break;

                case TSqlStatementType.sstInsert:
                    TInsertSqlStatement insertstmt = (TInsertSqlStatement)pstmt;
                     p = "";

                     for (int i = 0; i < insertstmt.Tables.Count(); i++)
                    {
                        p = p + insertstmt.Tables[i].AsText;
                        if (i != insertstmt.Tables.Count() - 1)
                        {
                            p = p + ",";
                        }
                    }

                    Console.WriteLine("insert statement: {0}", p);
                    break;

                case TSqlStatementType.sstUpdate:
                    TUpdateSqlStatement updatestmt = (TUpdateSqlStatement)pstmt;
                    p = "";

                    for (int i = 0; i < updatestmt.Tables.Count(); i++)
                    {
                        p = p + updatestmt.Tables[i].AsText;
                        if (i != updatestmt.Tables.Count() - 1)
                        {
                            p = p + ",";
                        }
                    }

                    Console.WriteLine("update statement: {0}", p);
                    break;

                case TSqlStatementType.sstMssqlCreateProcedure:
                    Console.WriteLine("create procedure: {0}", ((TMssqlCreateProcedure)pstmt).procedurename.AsText);
                    break;
                case TSqlStatementType.sstMssqlAlterProcedure:
                    Console.WriteLine("alter procedure: {0}", ((TMssqlCreateProcedure)pstmt).procedurename.AsText);
                    break;
                case TSqlStatementType.sstMssqlCreateFunction:
                    Console.WriteLine("create function: {0}", ((TMssqlCreateFunction)pstmt).functionname.AsText);
                    break;
                case TSqlStatementType.sstMssqlAlterFunction:
                    Console.WriteLine("alter function: {0}", ((TMssqlCreateFunction)pstmt).functionname.AsText);
                    break;
                case TSqlStatementType.sstMssqlDropProcedure:
                    TMssqlDropProcedure dropProcedure = (TMssqlDropProcedure)pstmt;
                    for (int i = 0; i < dropProcedure.lstProcedures.Count(); i++)
                    {
                        Console.WriteLine("drop procedure: {0}", dropProcedure.lstProcedures[i].AsText);
                    }
                    break;
                case TSqlStatementType.sstMssqlGrant:
                    p="";
                    
                    for (int i = 0; i < ((TMssqlGrant)pstmt).principals.Count(); i++)
                    {
                        p = p + ((TMssqlGrant)pstmt).principals[i].AsText;
                        if (i != ((TMssqlGrant)pstmt).principals.Count() - 1) {
                            p = p +",";
                        }
                    }

                    Console.WriteLine("grant {0} to {1}",((TMssqlGrant)pstmt).securable.AsText,p);
                    break;
                default:
                    //Console.WriteLine(pstmt.ToString());
                    break;
            }

            for (int j = 0; j < pstmt.ChildNodes.Count(); j++)
            {
                if (pstmt.ChildNodes[j] is TCustomSqlStatement)
                {
                    checkStmt((TCustomSqlStatement)(pstmt.ChildNodes[j]));
                }
            }

        }

    }
}
